package com.medishare.adapter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.medishare.R;
import com.medishare.activity.MainActivity;
import com.medishare.data.DataDoctorNotice;
import com.medishare.data.DataHeathTip;
import com.medishare.data.DataMediReminder;
import com.medishare.data.DataVaccineNotice;
import com.medishare.fragment.FragmentCallCenter;
import com.medishare.fragment.ProgressDialogFragment;
import com.medishare.util.Utils;

import android.app.Activity;
import android.app.DialogFragment;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AdapterVaccineNotice extends BaseAdapter {
	private Activity mContext;
	private ArrayList<DataVaccineNotice> mData;
	private LayoutInflater mInflater;
    public AdapterVaccineNotice(Activity context,ArrayList<DataVaccineNotice> datas){
    	this.mContext = context;
    	this.mData = datas;
    	mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public DataMediReminder getItem(int position) {
		mData.get(position);
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		DataVaccineNotice data = mData.get(position);
		if(convertView == null){
			convertView = mInflater.inflate(R.layout.listview_item_vaccine_notice, null);
			holder = new ViewHolder();
			holder.createTime = (TextView) convertView.findViewById(R.id.create_time);
			holder.personName = (TextView) convertView.findViewById(R.id.person_name);
			holder.vaccineName = (TextView) convertView.findViewById(R.id.vaccine_name);
			holder.planedTime = (TextView) convertView.findViewById(R.id.planed_time);
			holder.remark = (TextView) convertView.findViewById(R.id.remark);
			holder.confirm = (Button) convertView.findViewById(R.id.confirm);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		holder.createTime.setText(Utils.convertTime(data.getUpdate_time()));
		holder.personName.setText(data.getName());
		holder.vaccineName.setText(data.getVaccine_name());
		holder.planedTime.setText(data.getPlan_date());
		holder.remark.setText(data.getRemark());
		onClick(holder.confirm,data.getId());
		return convertView;
	}
    
	private DialogFragment dialog;
	private void onClick(Button confirm, final String id) {
		confirm.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog = ProgressDialogFragment.newInstance();
				dialog.show(mContext.getFragmentManager().beginTransaction(), "progress");
				String url = Utils.getBaseUrl() + "Vaccine/confirm/?id=" + id;
				AsyncHttpClient client = new AsyncHttpClient();
				client.get(url, new AsyncHttpResponseHandler(){
					Message msg = new Message();
					@Override
					public void onSuccess(String content)  {
						super.onSuccess(content);
						msg.what = 0;
						mHandler.sendMessage(msg);
					}
					@Override
					public void onFailure(Throwable error, String content) {
						msg.what = -1;
						mHandler.sendMessage(msg);
						super.onFailure(error, content);
					}
				});
			}
		});
		
	}


	static class ViewHolder{
		public TextView createTime;
		public TextView personName;
		public TextView vaccineName;
		public TextView planedTime;
		public TextView remark;
		public Button confirm;
	}
	
	
	Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (dialog != null && dialog.getDialog().isShowing()) {
				dialog.dismiss();
			}
			switch(msg.what){
			case 0:
				Intent i = new Intent("com.medishare.vaccine");
				mContext.sendBroadcast(i);
				break;
			case -1:
				Toast.makeText(mContext, mContext.getResources().getString(R.string.server_error), 1).show();
				break;
			}
			}
    };
}
